package zekezang.org;

import java.io.File;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

public class UpdateReceiver extends BroadcastReceiver {
	public static final int NOTIFICATION_ID = 10001;
	private Context context = null;

	@Override
	public void onReceive(Context context, Intent intent) {
		this.context = context;
		System.out.println("come herer rece");
		Toast.makeText(context, "下载完成啦,到通知栏更新吧", Toast.LENGTH_LONG).show();
		showNotification(intent.getStringExtra("path"));
	}

	private void showNotification(String saveFileName) {
		final File apkfile = new File(saveFileName);
		
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");

		Notification notification = new Notification(R.drawable.icon, "下载完成啦，点击更新", System.currentTimeMillis());
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, 0);

		notification.setLatestEventInfo(context, "下载完成啦，点击更新", "点吧，，，更猛烈一点", contentIntent);

		NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

		notificationManager.notify(NOTIFICATION_ID, notification);

	}

//	// 利用广播实现
//	private void installApk(String saveFileName, Context context) {
//		final File apkfile = new File(saveFileName);
//
//		AlertDialog.Builder builderUpdate = new AlertDialog.Builder(context);
//		builderUpdate.setTitle("版本更新");
//		builderUpdate.setMessage("下载完成, 立即更新?");
//		builderUpdate.setNegativeButton("是", new DialogInterface.OnClickListener() {// 退出按钮
//					@Override
//					public void onClick(DialogInterface dialog, int ii) {
//						if (!apkfile.exists()) {
//							return;
//						}
//						Intent i = new Intent(Intent.ACTION_VIEW);
//						i.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
//						dialog.dismiss();
//					}
//				});
//		builderUpdate.setPositiveButton("否", new DialogInterface.OnClickListener() {
//			public void onClick(DialogInterface dialog, int which) {
//				dialog.dismiss();
//			}
//		});
//		AlertDialog dialogUpdate = builderUpdate.create();
//		if (dialogUpdate.isShowing()) {
//			dialogUpdate.dismiss();
//		}
//		dialogUpdate.show();// 显示对话框
//
//	}

}
